package com.ms.game.lobby.terminatedaccountlist;

import java.util.ArrayList;
import java.util.TreeMap;

import com.ms.game.lobby.terminatedaccountlist.db.TerminatedAccountListModel;
import com.ms.game.lobby.terminatedaccountlist.message.SGetTerminatedAccountListResponse;
import com.ms.game.lobby.terminatedaccountlist.message.TerminatedAccountData;
import com.ms.service.error.ErrorCode;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.message.MessageAction;

/**
 * 停權名單
 * @author TW16028
 *
 */
public class TerminatedAccountList {
	
	
	/** 
	 * startTime / dbId / model
	 */
	private TreeMap<Long, TreeMap<Integer, TerminatedAccountListModel>> terminatedAccountDatas = new TreeMap<Long, TreeMap<Integer, TerminatedAccountListModel>>();
	
	public void initList(){
		  
		ArrayList<TerminatedAccountListModel> models = TerminatedAccountListDAO.loadAllModels();
		for (TerminatedAccountListModel model : models) {

			Long startTime = model.getStartTime();

			if (!terminatedAccountDatas.containsKey(startTime)) {
				TreeMap<Integer, TerminatedAccountListModel> info = new TreeMap<Integer, TerminatedAccountListModel>();
				info.put(model.getId(), model);

				continue;
			}
			
			TreeMap<Integer, TerminatedAccountListModel> infos = terminatedAccountDatas.get(model.getStartTime());
			infos.put(model.getId(), model);
		}

	}
	
	/**
	 * 讀取指定時間跟數量的停權名單</br>
	 * 並且送給Client</br>
	 * @param sessionId
	 * @param time
	 * @param size
	 */
	public static void loadAndSendToClient(GUID sessionId, int page, int numOfPage) {
		ArrayList<TerminatedAccountData> datas = new ArrayList<TerminatedAccountData>();
		int totalPage = TerminatedAccountListDAO.getPageNum(numOfPage);
		if(page > totalPage){
			page = totalPage;
		}
		
		int from = (page - 1)  * numOfPage;
		ArrayList<TerminatedAccountListModel> models = TerminatedAccountListDAO.loadModelByPage(numOfPage, from);
		for (TerminatedAccountListModel model : models) {
			datas.add(new TerminatedAccountData(model));
		}
		
		SGetTerminatedAccountListResponse response = new SGetTerminatedAccountListResponse();
		response.setErrorCode(ErrorCode.SUCCESS);
		response.setTerminatedAccountDatas(datas);
		response.setTotalPage(totalPage);
		MessageAction.sendMessage(sessionId, MessageTypes.S_LOBBY_GET_TERMINATED_ACCOUNT_LIST_RESPONSE, response);
	}
	
}
